//
//  NoiseController.swift
//  DSP
//
//  Created by tucici on 2024/8/1.
//

import UIKit
import TCCKit
struct NoiseStruct{
    var name = ""
    var db = 0
    init(name: String = "", db: Int = 0) {
        self.name = name
        self.db = db
    }
}
class NoiseController: DSAlertController {
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var okBtn: UIButton!
    @IBOutlet weak var closeBtn: UIButton!
    @IBOutlet weak var line: UIView!
    @IBOutlet weak var line2: UIView!
    private var index = 100
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    override func initUI() {
        autoAddCollectionView = false
        collectionView.delegate = self
        collectionView.dataSource = self
        flowLayout.scrollDirection = .vertical
        collectionView.backgroundColor = .white
        collectionView.collectionViewLayout = NoiseFlowLayout()
        collectionView.register(nib: NoiseCell.self)
        bgView.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(line.snp.bottom)
            make.bottom.equalTo(line2.snp.top)
        }
    }
    
    override func initData() {
        array = [NoiseStruct(name: "Level 1", db: -91),
                 NoiseStruct(name: "Level 2", db: -88),
                 NoiseStruct(name: "Level 3", db: -85),
                 NoiseStruct(name: "Level 4", db: -82),
                 NoiseStruct(name: "Level 5", db: -79),
                 NoiseStruct(name: "Level 6", db: -76),
                 NoiseStruct(name: "Level 7", db: -73),
                 NoiseStruct(name: "Level 8", db: -70),
                 NoiseStruct(name: "Close", db: -135)]
    }
    var array = [NoiseStruct]() {
        didSet{
            collectionView.reloadData()
        }
    }
    
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        if sender == closeBtn{
            dismiss(animated: false)
        }else{
//            if index < array.count {
//               _ = BLEData.noise(array.count - 1 == index ? 0 : index).write()
//            }
            dismiss(animated: false)
        }
    }
}
//MARK: - UICollectionViewDataSource
extension NoiseController:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cell(NoiseCell.self, indexPath) as! NoiseCell
        let model = array[indexPath.row]
        cell.name.text = model.name
        cell.sub.text = "\(model.db)" + "dB"
        cell.borderWidth = 1
        cell.bgHexColor = indexPath.row == index ? HEX_RED : HEX_WHITE
        cell.borderHexColor = indexPath.row == index ? HEX_RED : HEX_BLACK
        cell.name.textHexColor = indexPath.row == index ? HEX_WHITE : HEX_BLACK
        cell.sub.textHexColor = indexPath.row == index ? HEX_WHITE : HEX_BLACK
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension NoiseController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        index = indexPath.row
        collectionView.reloadData()
        
        _ = BLEData.noise(array.count - 1 == index ? 0 : index+1).write()

    }
}
//MARK: - UICollectionViewDelegateFlowLayout
extension NoiseController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let w = (kUIScreenWidth - 60 - 60) / 2.0
        return CGSize(width: w, height: 30)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20)
    }
}


